package com.project.action;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.project.service.BaseService;
import com.project.service.UserService;
import com.project.util.StringUtil;
import com.project.vo.Admin;
import com.project.vo.User;

public class LoginAction extends ActionSupport {
	private Integer id;
	private String name;
	private String password;
	private Boolean isRememberId;
	private BaseService baseService;
	private UserService userService;
	private Map<String, Object> request;
	private Map<String, Object> session;
	
	public LoginAction() {
		ActionContext context = ActionContext.getContext();
		request = (Map<String, Object>) context.get(StringUtil.REQUEST);
		session = context.getSession();
	}
	
	public String login() {
		User user = userService.login(id, password);
		if (user.getId() == null) {
			request.put(StringUtil.RESULT, StringUtil.E_LOGIN);
			session.put(StringUtil.LOGINSTATE, StringUtil.LOGOUT);
			return StringUtil.FAILURE;
		}
		//add cookies
//		request.put(StringUtil.USERNAME, username);
//		request.put(StringUtil.PASSWORD, password);
		session.put(StringUtil.USER, user);
		session.put(StringUtil.LOGINSTATE, StringUtil.LOGIN);
		return StringUtil.SUCCESS;
	}
	public String login_A() {
		Admin admin = userService.login_A(id, password);
		if (admin.getId() == null) {
			request.put(StringUtil.RESULT, StringUtil.E_LOGIN);
			session.put(StringUtil.LOGINSTATE, StringUtil.LOGOUT);
			return StringUtil.FAILURE;
		}
		//add cookies
//		request.put(StringUtil.USERNAME, username);
//		request.put(StringUtil.PASSWORD, password);
		session.put(StringUtil.ADMIN, admin);
		session.put(StringUtil.LOGINSTATE, StringUtil.LOGIN);
		return StringUtil.SUCCESS;
	}	
	public String logout(){
		session.remove(StringUtil.USER);
		session.remove(StringUtil.ADMIN);
		session.put(StringUtil.LOGINSTATE, StringUtil.LOGOUT);
		if (session.get(StringUtil.USER) != null) {
			return StringUtil.FAILURE;
		}else if (session.get(StringUtil.ADMIN) != null) {
			return StringUtil.FAILURE;
		}else {
			return StringUtil.SUCCESS;
		}
	}
	
	public void validate() {
		//add validate
		super.validate();
		System.out.println("user = "+ id + "pass="+password);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public Boolean getIsRememberId() {
		return isRememberId;
	}

	public void setIsRememberId(Boolean isRememberId) {
		this.isRememberId = isRememberId;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}
